// pages/myincome/userincome/index.js
const app = getApp() // 引入app
import _request from '../../../utils/request.js'
import { getTime } from '../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    currentDate: new Date().getTime(),
    minDate: new Date(2019, 5, 1).getTime(),
    maxDate: new Date().getTime(),
    show: false,
    recomUserlist: [],
    userList: [],
    page: 1,
    pageNo: 1,
    monthincome: 0,
    gettime: '',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    nowtime: '',
    nowtime1: '',
    UserCountObj: null,
  },

  // 关闭窗口
  onClose() {
    this.setData({
      show: false
    })
  },

  // 展示
  onShowpopup() {
    this.setData({
      show: true
    })
  },

  // 获取时间
  onInputTime(event) {
    let time = getTime(event.detail, 4)
    let gettime = getTime(event.detail, 5)
    this.setData({
      currentDate: event.detail,
      gettime: gettime,
      nowtime1: time
    });
  },

  // 确定
  onType() {
    this.setData({
      show: false,
      recomUserlist: [],
      nowtime: this.data.nowtime1
    })
    this.postrRecomUser(this.data.page, this.data.gettime, false)
  },

  // 查看详情
  onDetails(e) {
    const { inviteUserId } = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/myincome/userincome/details/index?id=' + inviteUserId,
    })
  },

  // 获取收益 1门店 2美发师 3用户 4拓客
  postrRecomUser(page, month, show) {
    if (show) {
      wx.showNavigationBarLoading() // 顶部加载状态
    }
    _request.postObj('/recomUser/recommendIncomeByMonthCombine', {
      userId: wx.getStorageSync('userid'),
      month,
      page,
      roletype: "3",
    }).then((res) => {
      if(res.data.data.length > 0 && res.data.data != null){
        this.setData({
          recomUserlist: res.data.data.map(item => {
            item.time = getTime(item.time, 1)
            return item
          }),
          monthincome:res.data.data[0].monthincome
        })
      }
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }).catch((error) => {
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      console.log(error);
    })
  },

  // 获取收益 1门店 2美发师 3用户 4拓客
  postUserList(page, show) {
    if (show) {
      wx.showNavigationBarLoading() // 顶部加载状态
    }
    _request.postObj('/recomUser/recommendUserList', {
      userId: wx.getStorageSync('userid'),
      page,
      roletype: "3",
    }).then((res) => {
      if(res.data.data.userIncomeInfos.length > 0 && res.data.data.userIncomeInfos != null){
        let list = res.data.data.userIncomeInfos.map(item => {
          item.inviteTime = getTime(item.inviteTime, 1)
          return item
        })
        this.setData({
          userList: page > 1 ?[...this.data.userList,...list] : list
          })
      }
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }).catch((error) => {
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      console.log(error);
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let time = getTime(new Date().getTime(), 4)
    this.setData({
      nowtime: time
    })
    let gettime = getTime(new Date().getTime(), 5)
    this.setData({
      gettime
    })
    this.postrRecomUser(this.data.page, gettime, false)

    this.postUserList(this.data.pageNo,false)
    //用户总推荐
    _request.get('/recommentIncomeCount/RecommendUserCount', {
      userId: wx.getStorageSync('userid'),
    }).then((res) => {
      this.setData({
        UserCountObj: res.data.data
      })
    }).catch((error) => {
      console.log(error);
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.postUserList(1,true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      pageNo: this.data.pageNo + 1
    })
    this.postUserList(this.data.pageNo,true)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})